package com.lab.tools;

import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.logtrack.AppLog;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.DisplayUtil;

/**
 * 桌面悬浮窗
 * Created by ak
 */
public class FloatWindowView extends IconTextView {

    private Context mContext;


    /**
     * 记录屏幕的宽度
     */
    private int mScreenWidth;

    /**
     * 记录当前手指位置在屏幕上的横坐标值
     */
    private float xInScreen;

    /**
     * 记录当前手指位置在屏幕上的纵坐标值
     */
    private float yInScreen;

    /**
     * 记录手指按下时在屏幕上的横坐标的值
     */
    private float xDownInScreen;

    /**
     * 记录手指按下时在屏幕上的纵坐标的值
     */
    private float yDownInScreen;

    /**
     * 记录手指按下时在小悬浮窗的View上的横坐标的值
     */
    private float xInView;

    /**
     * 记录手指按下时在小悬浮窗的View上的纵坐标的值
     */
    private float yInView;

    /**
     * 用于更新小悬浮窗的位置
     */
    private WindowManager mWindowManager;

    /**
     * 小悬浮窗的参数
     */
    private WindowManager.LayoutParams mParams;

    public FloatWindowView(Context context) {
        super(context);
        mContext = context;
        setImageRes();
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度
                setImageRes();
                xInView = event.getX();
                yInView = event.getY();
                xDownInScreen = event.getRawX();
                yDownInScreen = event.getRawY() - getStatusBarHeight();
                xInScreen = event.getRawX();
                yInScreen = event.getRawY() - getStatusBarHeight();
                break;
            case MotionEvent.ACTION_MOVE:
                xInScreen = event.getRawX();
                yInScreen = event.getRawY() - getStatusBarHeight();
                // 手指移动的时候更新小悬浮窗的位置
                updateViewPosition();
                break;
            case MotionEvent.ACTION_UP:
                // 如果手指离开屏幕时，xDownInScreen和xInScreen相等，且yDownInScreen和yInScreen相等，则视为触发了单击事件。
                int slop = ViewConfiguration.get(getContext()).getScaledWindowTouchSlop();
                if (Math.pow(xDownInScreen - xInScreen, 2) + Math.pow(yDownInScreen - yInScreen, 2)
                        < Math.pow(slop / 2, 2)) {
                    if (mContext != null) {
                        Intent intent = new Intent();

                        Intent activityIntent = new Intent(AppCoreRuntime.context, LogMainActivity.class);
                        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        AppCoreRuntime.context.startActivity(activityIntent);

//                        FloatWindowManager.showFloatMainWindowView(mContext.getApplicationContext());
                    }
                }
                refreshViewPosition();
                setImageRes();
                break;
            default:
                break;
        }
        return true;
    }

    private void setImageRes() {
        setText(R.string.message_pao_icon);
        setTextSize(30);
        setTextColor(ResourceUtil.getThemeColor());
    }

    /**
     * 将小悬浮窗的参数传入，用于更新小悬浮窗的位置。
     *
     * @param params 小悬浮窗的参数
     */
    public void setParams(WindowManager.LayoutParams params) {
        mParams = params;
    }

    /**
     * 更新小悬浮窗在屏幕中的位置。
     */
    private void updateViewPosition() {
        mParams.x = (int) (xInScreen - xInView);
        mParams.y = (int) (yInScreen - yInView);
        try {
            mWindowManager.updateViewLayout(this, mParams);
        } catch (IllegalArgumentException e) {
            AppLog.w("float view not attached");
        }
    }

    /**
     * 松开时刷新悬浮窗位置
     */
    private void refreshViewPosition() {
        mParams.x = mParams.x > mScreenWidth / 2 ? mScreenWidth : 0;
        try {
            mWindowManager.updateViewLayout(this, mParams);
        } catch (IllegalArgumentException e) {
            AppLog.w("float view not attached");
        }
    }

    /**
     * 用于获取状态栏的高度。
     *
     * @return 返回状态栏高度的像素值。
     */
    private int getStatusBarHeight() {
        return DisplayUtil.getStatusBarHeight();
    }

}
